package com.open.capacity.finance.service.impl;

import cn.hutool.core.lang.Assert;
import com.open.capacity.finance.dao.AuditModelDao;
import com.open.capacity.finance.entity.AuditModel;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.AuditModelRule;
import com.open.capacity.finance.dao.AuditModelRuleDao;
import com.open.capacity.finance.service.AuditModelRuleService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class AuditModelRuleServiceImpl implements AuditModelRuleService {

    @Autowired
    private AuditModelRuleDao auditModelRuleDao;

    @Autowired
    private AuditModelDao auditModelDao;


    /**
     * 添加
     *
     * @param auditModelRule
     */
    @Transactional
    public int save(AuditModelRule auditModelRule) {
        AuditModel model = auditModelDao.findById(auditModelRule.getModelId());
        Assert.notNull(model, "找不到对应的模型");

//        AuditModelRule origData = auditModelRuleDao.findByModelId(auditModelRule.getModelId());
//        Assert.isNull(origData, "不能重复配置模型预警规则");

        auditModelRule.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        auditModelRule.setStatus(1);
        auditModelRule.setUpdateTime(new Date());


        return auditModelRuleDao.save(auditModelRule);
    }

    /**
     * 修改
     *
     * @param auditModelRule
     */
    @Transactional
    public int update(AuditModelRule auditModelRule) {
        auditModelRule.setUpdateTime(new Date());


        return auditModelRuleDao.update(auditModelRule);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return auditModelRuleDao.delete(id);
    }

    /**
     * 查询记录
     */
    public AuditModelRule findByConfKey(String modelId, String confKey) {
        AuditModelRule data = auditModelRuleDao.findByConfKey(modelId, confKey);
        return data;
    }

    @Override
    public List<AuditModelRule> findByModelId(String modelId) {
        List<AuditModelRule> data = auditModelRuleDao.findByModelId(modelId);
        return data;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<AuditModelRule> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<AuditModelRule> list = auditModelRuleDao.findAll(params);
        PageInfo<AuditModelRule> pageInfo = new PageInfo(list);

        return PageResult.<AuditModelRule>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<AuditModelRule> findAll(Map<String, Object> params) {
        return auditModelRuleDao.findAll(params);
    }

}
